function addTr(tab, row, trHtml){
     //获取table最后一行 $("#tab tr:last")
     //获取table第一行 $("#tab tr").eq(0)
     //获取table倒数第二行 $("#tab tr").eq(-2)
     var $tr=$("#"+tab+" tr:last");
     if($tr.size()==0){
        alert("指定的table id或行数不存在！");
        return;
     }
     $tr.after(trHtml);
  }
   
  function delTr(ckb){
     //获取选中的复选框，然后循环遍历删除
     var ckbs=$("input[name="+ckb+"]:checked");
     if(ckbs.size()==0){
        alert("要删除指定行，需选中要删除的行！");
        return;
     }
	 var i=0;
     ckbs.each(function(i,ckb){
		$("#othertable tr").eq($(ckb).val()-i).remove();
		i++;
     });
  }

  
   
  function addTr2(tab, row){
    var trHtml="<tr id='rowsign'><td><input type='checkbox' id='inlineCheckbox1'  name='ckb' value='"+($("#othertable").find("tr").length)+"'></td>"+
			   "<td class='center'><input class='input-small focused' id='other_costname'  name='records.otherRecord.itemsList["+($("#othertable").find("tr").length-1)+"].costname' type='text' value=''></td>"+
			   "<td class='center'><input class='input-small focused' id='other_costdescription' name='records.otherRecord.itemsList["+($("#othertable").find("tr").length-1)+"].costdescription' type='text' value=''></td>"+
			   "<td class='center'><input class='input-small focused' oninput='rowcount(this)' id='other_amount' name='records.otherRecord.itemsList["+($("#othertable").find("tr").length-1)+"].amount' type='text' value=''></td>"+
			   "<td class='center'><input class='input-small focused' oninput='rowcount(this)' id='other_adjust' name='records.otherRecord.itemsList["+($("#othertable").find("tr").length-1)+"].adjust' type='text' value=''></td>"+
			   "<td class='center'><input class='input-small focused' readonly id='other_item_combined' name='records.otherRecord.itemsList["+($("#othertable").find("tr").length-1)+"].combined' type='text' value=''></td></tr>";
    addTr(tab, row, trHtml);
  }
   
  function delTr2(){
 // alert($("#dynamictable").find("tr").length);
     delTr('ckb');
	 $("#othertable").find("tr").each(function(i,trobj){
	    if(i>0){
		   $(trobj).find("td").eq(0).find("input").eq(0).val(i);
		}
	 });
  }
function saveFees(){
		$('#myModal').modal('show');
		var feesForm = $("#feesForm").formToArray(true);
		$.post("../fees/save_Records.action",feesForm,function(result){
			if(result.msg=="succ"){
	  			$('#myModal').modal('hide');
	  			$("#alertinfo").html("收费维护成功");
	  			$('#myinfo').modal('show');
	  			$("#closewin").bind("click",function(){
					window.location.reload();
				});
	  		}else{
	  			$('#myModal').modal('hide');
	  			$("#alertinfo").html(result.msg);
	  			$('#myinfo').modal('show');
	  		}
		});
};
function countfees(obj){
	var id=$(obj).attr("id");
	//护楼费
	if("hlf_fees"==id){
		if(checknum($(obj).val())){
			var hlf_combined=$("#hlf_combined").val();
			if(hlf_combined==""){
				hlf_combined=0;
			}
			$("#hlf_realfees").val(parseFloat(hlf_combined,10)-parseFloat($(obj).val(),10));
		}
	}
	//管理费
	if("glf_tfees"==id){
		if(checknum($(obj).val())){
			var glf_bsum=$("#glf_bsum").val();
			if(glf_bsum==""){
				glf_bsum=0;
			}
			$("#glf_latefees").val(parseFloat(glf_bsum,10)-parseFloat($(obj).val(),10));
		}
	}
	//暖气费
	if("nqf_bsum"==id){
		if(checknum($(obj).val())){
			var nqf_fees=$("#nqf_fees").val();
			if(nqf_fees==""){
				nqf_fees=0;
			}
			var nqf_latefines=$("#nqf_latefines").val();
			if(nqf_latefines==""){
				nqf_latefines=0;
			}
			$("#nqf_combined").val(parseFloat($(obj).val(),10)-parseFloat(nqf_fees,10)+parseFloat(nqf_latefines,10));
		}
	}
	if("nqf_fees"==id){
		if(checknum($(obj).val())){
			var nqf_bsum=$("#nqf_bsum").val();
			if(nqf_bsum==""){
				nqf_bsum=0;
			}
			var nqf_latefines=$("#nqf_latefines").val();
			if(nqf_latefines==""){
				nqf_latefines=0;
			}
			$("#nqf_combined").val(parseFloat(nqf_bsum,10)-parseFloat($(obj).val(),10)+parseFloat(nqf_latefines,10));
		}
	}
	if("nqf_payment"==id){
		if(checknum($(obj).val())){
			$("#nqf_latefines").val($("#nqf_payment").val());
		}
	}
	if("nqf_latefines"==id){
		if(checknum($(obj).val())){
			var nqf_bsum=$("#nqf_bsum").val();
			if(nqf_bsum==""){
				nqf_bsum=0;
			}
			var nqf_fees=$("#nqf_fees").val();
			if(nqf_fees==""){
				nqf_fees=0;
			}
			$("#nqf_combined").val(parseFloat(nqf_bsum,10)-parseFloat(nqf_fees,10)+parseFloat($(obj).val(),10));
		}
	}
	//房费
	if("ff_currentcost"==id){
		if(checknum($(obj).val())){
			var ff_setupcost=$("#ff_setupcost").val();
			if(ff_setupcost==""){
				ff_setupcost=0;
			}
			$("#ff_combined").val(parseFloat($(obj).val(),10)-parseFloat(ff_setupcost,10));
		}
	}
	if("ff_setupcost"==id){
		if(checknum($(obj).val())){
			var ff_currentcost=$("#ff_currentcost").val();
			if(ff_currentcost==""){
				ff_currentcost=0;
			}
			$("#ff_combined").val(parseFloat(ff_currentcost,10)-parseFloat($(obj).val(),10));
		}
	}
	countsum();
}
function rowcount(obj){
	var trobj=$(obj).closest("#rowsign");
	var other_amount=$(trobj.find("#other_amount")[0]).val();
	var other_adjust=$(trobj.find("#other_adjust")[0]).val();
	if(other_amount=='' || !checknum(other_amount)){
		other_amount='0';
	}
	if(other_adjust=='' || !checknum(other_adjust)){
		other_adjust='0';
	}
	$(trobj.find("#other_item_combined")[0]).val(other_amount-other_adjust);
	var count=0;
	for(var i=1;i<$("#othertable")[0].rows.length;i++){
		count=count+parseFloat($($($("#othertable")[0].rows[i]).find("#other_item_combined")[0]).val());
	}
	$("#other_combined").val(count);
	countsum();
}
function countsum(){
	var an_fees=$("#an_receivable").val()==""?0:$("#an_receivable").val();
	var water_fees=$("#water_receivable").val()==""?0:$("#water_receivable").val();
	var wsf_fees=$("#wsf_receivable").val()==""?0:$("#water_receivable").val();
	var hlf_fees=$("#hlf_realfees").val()==""?0:$("#hlf_realfees").val();
	var glf_fees=$("#glf_latefees").val()==""?0:$("#glf_latefees").val();
	var nqf_fees=$("#nqf_combined").val()==""?0:$("#nqf_combined").val();
	var ff_fees=$("#ff_combined").val()==""?0:$("#ff_combined").val();
	var other_fees=$("#other_combined").val()==""?0:$("#other_combined").val();
	$("#bfees").val(parseFloat(an_fees)+parseFloat(water_fees)+parseFloat(wsf_fees)+
			parseFloat(hlf_fees)+parseFloat(glf_fees)+parseFloat(nqf_fees)+parseFloat(ff_fees)+parseFloat(other_fees));
	var bfees=$("#bfees").val()==""?0:$("#bfees").val();
	var warrearage=$("#warrearage").val()==""?0:$("#warrearage").val();
	$("#shouldpay").val(parseFloat(bfees)+parseFloat(warrearage));
}
function selectchanage(obj){
	var toll=$(obj).val();
	if(toll=='1'){/*按面积*/
		$("#ff_currentcost").attr("readonly",true);
		$("#ff_currentcost").val($("#ff_area").val()*$("#ff_price").val())
	}else{
		$("#ff_currentcost").attr("readonly",false);
	}
}
function savefees(){
	$("#modal_frcode").val($("#frcode").val());
	$("#modal_bfees").val($("#bfees").val());
	$("#modal_warrearage").val($("#warrearage").val());
	$("#modal_shouldpay").val($("#shouldpay").val());
	$('#savefees').modal('show');
}
function charge(){
	$("#modal_charge").val(parseFloat($("#modal_realpay").val())-parseFloat($("#modal_shouldpay").val()));
}
function confirm(){
	$('#myModal').modal('show');
	var frcode=$("#frcode").val();
	var realpay=$("#modal_realpay").val();
	var charge=$("#modal_charge").val();
	var pname=$("#modal_pname").val();
	var content=$("#modal_content").val();
	$.post("../fees/confirm_Records.action",{"records.frcode":frcode,"records.realpay":realpay,"records.charge":charge,
		"records.pname":pname,"records.content":content},function(result){
			if(result.msg=="succ"){
	  			$('#myModal').modal('hide');
	  			$("#alertinfo").html("收费成功");
	  			$('#myinfo').modal('show');
	  		}else{
	  			$('#myModal').modal('hide');
	  			$("#alertinfo").html(result.msg);
	  			$('#myinfo').modal('show');
	  		}
		});
	
}
function printfees(){
	$('#savefees').modal('hide');
	var rid=$("#rid").val();
	var ymonth=$("#ymonth").val();
	$('#bodycontent').html('正在生成打印数据，请稍等...');
	$('#myModal').modal('show');
	$("#print_fees").attr("src","../fees/printFees_Records.action?unitid="+rid+"&ymonth="+ymonth);
	
}

function checknum(param){
	var reg = new RegExp("^[0-9]+(.[0-9]{2})?$");  
	if(!reg.test(param)){  
		alert("请输入最多带有两位小数的数字。例如：19.98"); 
		return false;
	}  
	return true;
}